import { Injectable } from '@nestjs/common'
import { LevelOneSubject } from './entities/level-one-subject.entity'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'
import { CreateLevelOneSubjectDto } from './dto/create-level-one-subject.dto'
@Injectable()
export class LevelOneSubjectService {
  constructor(@InjectRepository(LevelOneSubject) private levelOneSubject: Repository<LevelOneSubject>) {}
  async create(CreateLevelOneSubjectDto: CreateLevelOneSubjectDto) {
    await this.levelOneSubject.save(CreateLevelOneSubjectDto)
    return '创建成功'
  }

  async findAll() {
    const subject_data = await this.levelOneSubject.find()
    return subject_data
  }

  async update(CreateLevelOneSubjectDto: CreateLevelOneSubjectDto) {
    await this.levelOneSubject.update(CreateLevelOneSubjectDto._id, {
      name: CreateLevelOneSubjectDto.name,
      key: CreateLevelOneSubjectDto.key
    })
    return '更新成功'
  }

  async remove(id: any) {
    const subject_data = await this.levelOneSubject.findOne({ where: { _id: id } })
    // remove会触发级联操作 软删除后user和subject的第三张表还是有数据的
    await this.levelOneSubject.softRemove(subject_data)
    return '删除成功'
  }
}
